package com.atwork.wuhua.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.atwork.wuhua.ConstantsMyself;
import com.atwork.wuhua.R;
import com.atwork.wuhua.base.BaseActivity;
import com.atwork.wuhua.dialog.CenterDialog;
import com.atwork.wuhua.event.LoginSuccessEvent;
import com.atwork.wuhua.mvp.presenter.SetUpPresenter;
import com.atwork.wuhua.mvp.view.ISetUpActivity;
import com.atwork.wuhua.utils.DataCleanManager;
import com.atwork.wuhua.utils.LogUtils;
import com.atwork.wuhua.utils.UpdataAppUtil.APKVersionCodeUtils;
import com.atwork.wuhua.utils.UserUtil;
import com.bumptech.glide.Glide;
import com.suke.widget.SwitchButton;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetUpActivity extends BaseActivity implements ISetUpActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.switchbtn)
    SwitchButton switchbtn;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.tv_cache)
    TextView tvCache;
    @BindView(R.id.rly_login_out)
    RelativeLayout rlyLoginOut;

    private SetUpPresenter presenter;

    @Override
    protected boolean isBindEventBus() {
        return false;
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_setup;
    }

    @Override
    public void initView() {
        tvTitle.setText("设置");
        presenter = new SetUpPresenter();
        presenter.attachView(this);
        tvCache.setText(DataCleanManager.getTotalCacheSize(this));
        tvVersion.setText("当前版本" + APKVersionCodeUtils.getVerName(this));

        if (UserUtil.isLogin(this)) {
            rlyLoginOut.setVisibility(View.VISIBLE);
        } else {
            rlyLoginOut.setVisibility(View.GONE);
        }

        switchbtn.setChecked(true);
        switchbtn.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                LogUtils.e("isChecked==>" + isChecked);
            }
        });

    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.img_left, R.id.rly_abount, R.id.rly_version, R.id.rly_cache, R.id.rly_login_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_left:
                finish();
                break;
            case R.id.rly_abount:
                Intent intent = new Intent(this, AboutActivity.class);
                startActivity(intent);
                break;
            case R.id.rly_version:
                break;
            case R.id.rly_cache:
                try {
                    if (Looper.myLooper() == Looper.getMainLooper()) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                Glide.get(SetUpActivity.this).clearDiskCache();//清理磁盘缓存 需要在子线程中执行
                            }
                        }).start();
                        if (Looper.myLooper() == Looper.getMainLooper()) { //清理内存缓存  可以在UI主线程中进行
                            Glide.get(SetUpActivity.this).clearMemory();
                        }
                    } else {
                        Glide.get(SetUpActivity.this).clearDiskCache();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                DataCleanManager.cleanInternalCache(this);
                showToast("清理缓存成功");
                tvCache.setText("0K");
                break;
            case R.id.rly_login_out:
                presenter.setLogout();
                break;
        }
    }

    @Override
    public void onLogout() {
        EventBus.getDefault().post(new LoginSuccessEvent());
        rlyLoginOut.setVisibility(View.GONE);
    }
}
